package com.xyzwps.paimon.furina.common

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.xyzwps.paimon.furina.common.value.ValueModule

object JSON {
    val OM: ObjectMapper = ObjectMapper()
        .registerModules(
            KotlinModule.Builder()
                .enable(KotlinFeature.NullToEmptyMap)
                .enable(KotlinFeature.NullToEmptyCollection)
                .enable(KotlinFeature.NullIsSameAsDefault)
                .enable(KotlinFeature.StrictNullChecks)
                .enable(KotlinFeature.StrictNullChecks)
                .build()
        )
        .registerModules(ValueModule())

    fun stringify(data: Any?): String = OM.writeValueAsString(data)

    fun <T> parse(str: String, typeReference: TypeReference<T>): T = OM.readValue(str, typeReference)
}